package com.qianguo.shop.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.eva.androidlibrary.utils.UIUtil;
import com.eva.androidlibrary.widget.MaterialDialog;
import com.eva.androidlibrary.widget.mdprogressdialog.MdProgressDialog;
import com.qianguo.shop.R;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.UiThread;

/**
 * 公用方法工具类
 *
 * @author GUXIA318
 * @date 2015-09-18 13:54
 */
@EBean(scope = EBean.Scope.Singleton)
public class BaseUtil implements IBaseMethod {

    private static Toast sToast;

    private MaterialDialog mMaterialDialog;
    private MdProgressDialog mProgressDialog;

    /**
     * 收起软键盘
     *
     * @param context
     */
    @Override
    public void closeKeyboard(@NonNull Activity context) {
        View v = context.getWindow().peekDecorView();
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    @Override
    public void showSnackBar(@NonNull Activity context, String content) {
        showSnackBar(context, content, "", null);
    }

    @UiThread
    @Override
    public void showSnackBar(@NonNull Activity context, String content, String actionText, View.OnClickListener listener) {
        closeKeyboard(context);
        Resources res = context.getResources();
        View root = UIUtil.getFirstChildView(context);
        Snackbar snackbar = Snackbar.make(root, content, Snackbar.LENGTH_LONG);
        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
        layout.setPadding(0, 25, 0, 25);
        TextView tvTips = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
//        tvTips.setTextSize(res.getDimensionPixelSize(R.dimen.primary_text_size));
        tvTips.setTextColor(res.getColor(R.color.text_icons_color));
        if (!TextUtils.isEmpty(actionText)) {
            snackbar.setAction(actionText, listener);
            snackbar.setActionTextColor(res.getColor(R.color.accent_color));
        }
        snackbar.show();
    }

    @Override
    public void showToast(@NonNull Context context, String content) {
        showToast(context, content, Gravity.NO_GRAVITY);
    }

    @UiThread
    @Override
    public void showToast(@NonNull Context context, String content, int gravity) {
        View view = getToastView(context, content);
        if (sToast == null) {
            sToast = new Toast(context);
        }
        sToast.setView(view);
        if (gravity != Gravity.NO_GRAVITY) {
            sToast.setGravity(gravity, 0, 0);
        } else {
            sToast.setGravity(Gravity.CENTER, 0, 0);
        }
        sToast.setDuration(Toast.LENGTH_LONG);
        sToast.show();
    }

    @UiThread
    @Override
    public void showLoadingDialog(@NonNull Context context) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }

        if (null != mProgressDialog && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }

        // TODO why try catch
        try {
            mProgressDialog = new MdProgressDialog(context);
            mProgressDialog.show();
        } catch (Exception e) {

        }
    }

    @UiThread
    @Override
    public void closeLoadingDialog() {
        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
        }
        mProgressDialog = null;
    }

    @Override
    public void showDialog(@NonNull Context context, String title, String warningMsg, View.OnClickListener confirmClickListener, View.OnClickListener cancelClickListener) {
        showDialog(context, title, warningMsg, "", "", confirmClickListener, cancelClickListener);
    }

    @UiThread
    @Override
    public void showDialog(@NonNull Context context, String title, String warningMsg, String confirm, String cancel, View.OnClickListener confirmClickListener, View.OnClickListener cancelClickListener) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }
        if (null != mMaterialDialog) {
            mMaterialDialog.dismiss();
            mMaterialDialog = null;
        }

        // TODO why try catch
        try {
            mMaterialDialog = new MaterialDialog(context);
            mMaterialDialog.setTitle(title);
            mMaterialDialog.setMessage(warningMsg);

            if (!TextUtils.isEmpty(confirm)) {
                mMaterialDialog.setConfirm(confirm);
            }

            if (!TextUtils.isEmpty(cancel)) {
                mMaterialDialog.setCancel(cancel);
            }

            mMaterialDialog.setOnComfirmClick(confirmClickListener);
            mMaterialDialog.setOnCancelClick(cancelClickListener);
            mMaterialDialog.show();
        } catch (Exception e) {
        }
    }

    @UiThread
    @Override
    public void showWarningDialog(@NonNull Context context, String title, String warningMsg, View.OnClickListener onOkBtnClickListener) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }
        if (null != mMaterialDialog) {
            mMaterialDialog.dismiss();
            mMaterialDialog = null;
        }
        try {
            mMaterialDialog = new MaterialDialog(context);
            mMaterialDialog.setTitle(title);
            mMaterialDialog.setMessage(warningMsg);
            mMaterialDialog.setOnComfirmClick(onOkBtnClickListener);
            mMaterialDialog.setCancelGone();
            mMaterialDialog.setmCanDismiss(false);
            mMaterialDialog.show();
        } catch (Exception e) {
        }
    }

    @UiThread
    @Override
    public void closeDialog() {
        if (null != mMaterialDialog) {
            mMaterialDialog.dismiss();
            mMaterialDialog = null;
        }
    }

    private View getToastView(Context context, String content) {
        View v = LayoutInflater.from(context).inflate(R.layout.layout_toast_diy, null);
        TextView textView = (TextView) v.findViewById(R.id.view_toast_content);
        textView.setText(content);
        return v;
    }
}
